package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.WorkspaceMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    WorkspaceMapper workspaceMapper;
    @Autowired
    OrderMapper orderMapper;
    @Override
    public BusinessDataVO businessData() {
        BusinessDataVO businessDataVO = new BusinessDataVO();
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        int newUsers = workspaceMapper.getNewUser(begin, end);//新增用户数
        int validOrderCount=orderMapper.getvalidOrderCount(begin,end, Orders.COMPLETED);//有效订单数
        int totalOrderCount=orderMapper.gettotalOrderCount(begin,end);
        Double orderCompletionRate=validOrderCount*1.0/totalOrderCount;//订单完成率
        if (totalOrderCount==0){
            orderCompletionRate=0.0;
        }
        Map map=new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",Orders.COMPLETED);
        Double turnover=orderMapper.getSumAmount(map);//营业额
        if (turnover==null){
            turnover=0.0;
        }
        int userCount=workspaceMapper.getUserCount(begin,end);//当天下单的用户个数
        Double unitPrice=0.0;
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
        int newUsers = workspaceMapper.getNewUser(begin, end);//新增用户数
        int validOrderCount=orderMapper.getvalidOrderCount(begin,end, Orders.COMPLETED);//有效订单数
        int totalOrderCount=orderMapper.gettotalOrderCount(begin,end);
        Double orderCompletionRate=validOrderCount*1.0/totalOrderCount;//订单完成率
        if (totalOrderCount==0){
            orderCompletionRate=0.0;
        }
        Map map=new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",Orders.COMPLETED);
        Double turnover=orderMapper.getSumAmount(map);//营业额
        if (turnover==null){
            turnover=0.0;
        }
        int userCount=workspaceMapper.getUserCount(begin,end);//当天下单的用户个数
        Double unitPrice=0.0;
        if (userCount!=0){
            unitPrice=turnover/userCount;
        }
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        int discontinued=workspaceMapper.getDiscontinued();//已停售
        int sold=workspaceMapper.getSold();//已起售
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        int discontinued=workspaceMapper.getDiscontinuedByDish();//已停售
        int sold=workspaceMapper.getSoldByDish();//已起售
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        int allOrders=workspaceMapper.getOrders();//总订单数
        int cancelledOrders=workspaceMapper.getCancelledOrders();//已取消订单
        int completedOrders=workspaceMapper.getCompletedOrders();//已完成订单
        int deliveredOrders=workspaceMapper.getDeliveredOrders();//带派送订单
        int waitingOrders=workspaceMapper.getWaitingOrders();//待接单订单
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
